/**
 * @Author：ming
 * @Date：2023/9/26 12:22 PM
 * @Filename：Lc290
 */

import java.util.HashMap;

/**
 * @author ming
 */
public class Lc290 {
    public boolean wordPattern(String pattern, String s) {
        String[] split = s.split(" ");
        char[] cc = pattern.toCharArray();

        if(split.length!=cc.length){
            return false;
        }

        HashMap<Character, String> map = new HashMap<>();

        for (int i = 0; i < cc.length; i++) {
            Character c1 = cc[i];
            String s1 = split[i];

            if(map.containsKey(c1)){
                if(map.get(c1).equals(s1)){
                    continue;
                }else {
                    return false;
                }
            }else {
                map.put(c1,s1);
            }
        }
        HashMap<String, Character> map1 = new HashMap<>();
        for (int i = 0; i < cc.length; i++) {
            Character c1 = cc[i];
            String s1 = split[i];

            if(map1.containsKey(s1)){
                if(map1.get(s1)==c1){
                    continue;
                }else {
                    return false;
                }
            }else {
                map1.put(s1,c1);
            }
        }


        return true;
    }
}
